package com.djsearch.data;

import java.util.Vector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.djsearch.bean.GoodsBaseBean;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.util.DefaultJSONData;

/**
 * 解析服务器端返回的json格式数据
 * @author chenyang2
 *
 */
public class ResolvJSONData {

	/**
	 * 解析一个josn对象
	 * 如：{“result”:”0”,“message”:”登陆成功” ,”userId”:”87653434”,”recommendPerson”:”king”,
	 * ”xianjiinMoney”：”100”,”jiangjinMoney”:”100”,”voucherMoney”:”100”}
	 * @throws JSONException
	 */
	public void parse() throws JSONException {
		// TODO Auto-generated method stub
		
	}
    
	/**
	 * 解析团购商品数据
	 * 解析含有n个json个对象的json数组的json对象
	 * 如：{“result”:”0”,”returnMessage”:[{ ”shopName”:”索爱手机”,”shopPrice”:”1000”,
	 * ”salePrice”:”800”,”sale”:”0.8”,”shopDescripe”:”索爱手机做工精美，购买人数众多”,
	 * ”shopDistance”:”1.1”,”smallImage”:http://smallImage.jpg,”bigImage”:http://bigImage.jpg,
	 * ”shopPlace”:”二七广场”,”telephone”:”0373-123456”,”hasShop”:”1234”,”shopUrl”:”http://tuangou.html”}]}
	 * @param url  请求路径
	 * @param groupBuyGoodsVector  放 团购商品的对象
	 * @throws JSONException
	 */
	public boolean parse(String url,Vector<GroupBuyGoods> groupBuyGoodsVector) throws JSONException {
		// TODO Auto-generated method stub
		String responseStr = HttpRequestManager.getInstance().getStringFromServer(url,50000);
		if (responseStr == null ||responseStr.equals("")) {
			return false ;
		}
		JSONObject jsonObject = new JSONObject(responseStr);
		//解析出上面的result值 ，如果为0 则成功，否则失败
		String isSuccess = jsonObject.getString("result");
		if (isSuccess.equals("0")) {
			JSONArray jsonArray = jsonObject.getJSONArray("returnMessage");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject obJsonObject = jsonArray.getJSONObject(i);
				GroupBuyGoods groupBuyGoods = new GroupBuyGoods();
				groupBuyGoods.setShopName(obJsonObject.getString("shopName"));
				groupBuyGoods.setShopPrice(obJsonObject.getString("shopPrice"));
				groupBuyGoods.setSalePrice(obJsonObject.getString("salePrice"));
				groupBuyGoods.setSale(obJsonObject.getString("sale"));
				groupBuyGoods.setShopDescripe(obJsonObject.getString("shopDescripe"));
				groupBuyGoods.setShopDistance(obJsonObject.getString("shopDistance"));
				groupBuyGoods.setSmallImage(obJsonObject.getString("smallImage"));
				groupBuyGoods.setBigImage(obJsonObject.getString("bigImage"));
				groupBuyGoods.setShopPlace(obJsonObject.getString("shopPlace"));
				groupBuyGoods.setTelephone(obJsonObject.getString("telephone"));
				groupBuyGoods.setHasShop(obJsonObject.getString("hasShop"));
				groupBuyGoods.setShopUrl(obJsonObject.getString("shopUrl"));
				groupBuyGoodsVector.add(groupBuyGoods);
			}
		}
		return true;
		
	}

	//parse {"password":"13da49c2955","result":"0","userId":"13da49aa2b5"}
	public boolean parseRegKey() throws JSONException{
		String s = HttpRequestManager.getInstance().
		getStringFromServer("http://116.255.134.52:8088/awp/jsp/cuser!oneKeyReg.shtml",60000);
		JSONObject jsonObject = new JSONObject(s);
		String password = jsonObject.getString("password");
		String result = jsonObject.getString("result");
		if (result.equals("0")) {
			return true;
		}
		return false;
		
	}
	
	
	public void parseStr(String result) throws JSONException {
		// TODO Auto-generated method stub
		
	}
	
	
	
}
